package cn.edu.neusoft.food729.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.retroiftdemo_729.R;

import java.util.List;

import cn.edu.neusoft.food729.Bean.OrdersBean;

public class OrdersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<OrdersBean> list;
    private Context context;
    private LayoutInflater layoutInflater;


    public OrdersAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<OrdersBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.recommend_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        OrdersBean entity = list.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.username.setText((CharSequence) entity.getFoodname());
        viewHolder.time.setText(entity.getOrder_time());
        viewHolder.comment.setText("总价格：" + String.valueOf(entity.getSum()) + "元");
    }

    @Override
    public int getItemCount() {
        if (list != null)
            return list.size();
        else
            return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView username, time, comment;

        public ViewHolder(View itemView) {
            super(itemView);

            username = itemView.findViewById(R.id.username);
            time = itemView.findViewById(R.id.time);
            comment = itemView.findViewById(R.id.comment);
        }
    }
}
